package com.yingxin.yxerp.module.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.constant.CommonConst;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yxerp.module.sys.entity.OtherSet;
import com.yingxin.yxerp.module.sys.mapper.OtherSetMapper;
import com.yingxin.yxerp.module.sys.service.IOtherSetService;
import com.yingxin.yxerp.module.sys.vo.req.OtherSetQueryVo;
import com.yingxin.yxerp.module.sys.vo.resp.OtherSetRespVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OtherSetServiceImpl
        extends ServiceImpl<OtherSetMapper, OtherSet>
        implements IOtherSetService {

    @Autowired
    private IUserSupport userSupport;

    /**
     * 获取 mapper
     * @return
     */
    @Override
    public OtherSetMapper mapper(){
        return baseMapper;
    }

    @Override
    public void create(OtherSet otherSet) {
        if(StringUtils.isEmpty( otherSet.getType() ) ){
            throw new RuntimeException("类型不能为空！");
        }
        // 验证name是否重复
        checkTypeNameRepeat( CommonConst.FALSE, otherSet );
        // 验证value是否重复
        checkTypeValueRepeat( CommonConst.FALSE, otherSet );

        otherSet.setCreatorId( userSupport.getUserId() );
        otherSet.setCreatorName( userSupport.getStaffName() );
        otherSet.setDelFlag( CommonConst.FALSE );

        save(otherSet);
    }

    @Override
    public void delete(String id) {
        OtherSet otherSet = getById(id);
        if(StringUtils.isEmpty( otherSet.getType() ) ){
            throw new RuntimeException("类型不能为空！");
        }
        if( otherSet.getIsDel().trim().equals( CommonConst.FALSE ) ){
            throw new BizException("初始数据不可删除！");
        }
        otherSet.setDelFlag( CommonConst.TRUE );
        updateById(otherSet);
    }

    @Override
    public void update(OtherSet otherSet) {
        // 验证name是否重复
        checkTypeNameRepeat( CommonConst.TRUE, otherSet );
        // 验证value是否重复
        checkTypeValueRepeat( CommonConst.TRUE, otherSet );

        OtherSet other = getById( otherSet.getId() );
        if( other.getIsEdit().trim().equals( CommonConst.FALSE ) ){
            throw new BizException("初始数据不可编辑！");
        }
        other.setName( otherSet.getName() );
        other.setValue( otherSet.getValue() );

        updateById( other );
    }

    /**
     * 验证对应的type 下的名称是否已经存在
     * @param addOrUpdate 0 = add， 1 = update
     * @param set
     */
    private void checkTypeNameRepeat(String addOrUpdate, OtherSet set){
        QueryWrapper<OtherSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq( OtherSet::getDelFlag, CommonConst.FALSE );
        queryWrapper.lambda().eq( OtherSet::getType, set.getType() );
        queryWrapper.lambda().eq( OtherSet::getName, set.getName() );
        if( CommonConst.TRUE.trim().equals( addOrUpdate ) ){
            queryWrapper.lambda().ne( OtherSet::getId, set.getId() );
        }
        List<OtherSet> lists = list( queryWrapper );
        if( CollectionUtil.isNotEmpty( lists ) ){
            throw new BizException("名称已经存在！");
        }
    }

    /**
     * 验证对应的type下的value是否已经存在
     * @param addOrUpdate 0 = add， 1 = update
     * @param set
     */
    private void checkTypeValueRepeat(String addOrUpdate, OtherSet set){
        QueryWrapper<OtherSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq( OtherSet::getDelFlag, CommonConst.FALSE );
        queryWrapper.lambda().eq( OtherSet::getType, set.getType() );
        queryWrapper.lambda().eq( OtherSet::getValue, set.getValue() );
        if( CommonConst.TRUE.trim().equals( addOrUpdate ) ){
            queryWrapper.lambda().ne( OtherSet::getId, set.getId() );
        }
        List<OtherSet> lists = list( queryWrapper );
        if( CollectionUtil.isNotEmpty( lists ) ){
            throw new BizException("值已经存在！");
        }
    }

    /**
     * 查询全部
     * @param queryVo
     */
    @Override
    public List<OtherSetRespVo> listVo(OtherSetQueryVo queryVo){
        return baseMapper.listVo(queryVo);
    }

    /**
     * 根据ID查询
     * @param id
     */
    @Override
    public OtherSetRespVo getVo(String id){
        return baseMapper.getVo( id );
    }

}
